//
//  AVAudioAddViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/28.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "AVAudioAddViewController.h"
#import <AVFoundation/AVFoundation.h>
#import "WTRecorder.h"



@interface AVAudioAddViewController ()

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)WTRecorder *wtrView;
@property (nonatomic,strong) AVAudioRecorder *audioRecorder;//音频录音机
@property (nonatomic,strong) AVAudioPlayer *audioPlayer;//音频播放器，用于播放录音文件
@property (nonatomic,strong) NSTimer *timer;//录音声波监控（注意这里暂时不对播放进行监控）
@end

@implementation AVAudioAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"音频添加";
    [self createAddAudioUI];
    // Do any additional setup after loading the view.
}
/**
 创建基础界面
 */
-(void)createAddAudioUI{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:headerView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(scaleTopAndBottom(70));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.height.mas_equalTo(scaleTopAndBottom(150));
    }];
    self.wtrView = [[WTRecorder alloc] initWithFrame:CGRectZero];
    [headerView addSubview:self.wtrView];
    [self.wtrView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headerView).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    UIView *bottomView = [[UIView alloc] init];
    bottomView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-5));
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.height.mas_equalTo(scaleTopAndBottom(150));
    }];
    [self addSubVIewInBottomView:bottomView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.equalTo(self.view.mas_left).offset(scaleLeftAndRight(10));
        make.right.equalTo(self.view.mas_right).offset(scaleLeftAndRight(-10));
        make.bottom.equalTo(bottomView.mas_top);
    }];
}

/**
 添加底部视图的按钮
 */
-(void)addSubVIewInBottomView:(UIView *)bgView{
    UIButton *recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [recordBtn addTarget:self action:@selector(startRecordAudio:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:recordBtn];
    [recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(bgView.mas_centerX);
        make.top.equalTo(bgView.mas_top).offset(scaleTopAndBottom(5));
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    UILabel *centerLabel = [[UILabel alloc] init];
    centerLabel.text = @"录音中......";
    centerLabel.textAlignment = NSTextAlignmentCenter;
    centerLabel.backgroundColor = [UIColor cyanColor];
    [bgView addSubview:centerLabel];
    [centerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(bgView);
        make.top.equalTo(recordBtn.mas_bottom).offset(scaleTopAndBottom(5));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:playBtn];
    UIButton *resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:resetBtn];
    UIButton *clipBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:clipBtn];
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:saveBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.left.equalTo(bgView.mas_left).offset(scaleLeftAndRight(20));
        make.top.equalTo(centerLabel.mas_bottom).offset(scaleTopAndBottom(5));
    }];
    [playBtn addTarget:self action:@selector(playRecordVideo:) forControlEvents:UIControlEventTouchUpInside];
    [resetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.left.equalTo(playBtn.mas_right).offset(scaleLeftAndRight(40));
        make.top.equalTo(centerLabel.mas_bottom).offset(scaleTopAndBottom(5));
    }];
    [clipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.left.equalTo(resetBtn.mas_right).offset(scaleLeftAndRight(40));
        make.top.equalTo(centerLabel.mas_bottom).offset(scaleTopAndBottom(5));
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(bgView.mas_bottom).offset(scaleTopAndBottom(-5));
        make.left.equalTo(clipBtn.mas_right).offset(scaleLeftAndRight(40));
        make.top.equalTo(centerLabel.mas_bottom).offset(scaleTopAndBottom(5));
         make.right.equalTo(bgView.mas_right).offset(scaleLeftAndRight(-20));
        make.width.equalTo(@[playBtn,resetBtn,clipBtn]);
    }];
    playBtn.backgroundColor = [UIColor redColor];
    clipBtn.backgroundColor = [UIColor redColor];
    resetBtn.backgroundColor = [UIColor redColor];
    saveBtn.backgroundColor = [UIColor redColor];
    recordBtn.backgroundColor = [UIColor purpleColor];
}
-(void)startRecordAudio:(id)sender{
    [self.wtrView clickedRecorderButton:sender];
}
-(void)playRecordVideo:(id)sender{
    [self.wtrView clickedVoicePlay:sender];
    NSLog(@"想播放");
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.wtrView.audioPlayer = nil;
}
@end
